package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.entity.SysAppMenu;
import org.jeecg.modules.system.entity.WmsPdaRoleMenu;
import org.jeecg.modules.system.service.ISysAppMenuService;
import org.jeecg.modules.system.service.IWmsPdaRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @Description: PDA菜单信息
* @Author: jeecg-boot
* @Date:   2023-02-21
* @Version: V1.0
*/
@Api(tags="PDA菜单信息")
@RestController
@RequestMapping("/basic/sysAppMenu")
@Slf4j
public class SysAppMenuController extends JeecgController<SysAppMenu, ISysAppMenuService> {
   @Autowired
   private ISysAppMenuService sysAppMenuService;
   @Autowired
   private IWmsPdaRoleMenuService wmsPdaRoleMenuService;

   /**
    * 分页列表查询
    *
    * @param sysAppMenu
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "PDA菜单信息-分页列表查询")
   @ApiOperation(value="PDA菜单信息-分页列表查询", notes="PDA菜单信息-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(SysAppMenu sysAppMenu,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<SysAppMenu> queryWrapper = QueryGenerator.initQueryWrapper(sysAppMenu, req.getParameterMap());
       Page<SysAppMenu> page = new Page<SysAppMenu>(pageNo, pageSize);
       IPage<SysAppMenu> pageList = sysAppMenuService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

    @GetMapping(value = "/showPermission")
    public Result<?> showPermission(String currentRoleId,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        //查询菜单角色表中绑定的菜单
        LambdaQueryWrapper<WmsPdaRoleMenu> query = new LambdaQueryWrapper<>();
        query.eq(WmsPdaRoleMenu::getRoleId,currentRoleId);
        List<WmsPdaRoleMenu> pdaMenus = wmsPdaRoleMenuService.list(query);
        List<String> menuIds = new ArrayList<>();
        //没有中间表数据暂时不查询
        if(!CollectionUtils.isEmpty(pdaMenus)){
            menuIds = pdaMenus.stream().map(WmsPdaRoleMenu::getMenuId).collect(Collectors.toList());
        }else{
            menuIds = Arrays.asList("-1");
        }
        SysAppMenu sysAppMenu = new SysAppMenu();
        QueryWrapper<SysAppMenu> queryWrapper = QueryGenerator.initQueryWrapper(sysAppMenu, req.getParameterMap());
        if(!CollectionUtils.isEmpty(menuIds)){
            queryWrapper.in("id",menuIds);
        }
        Page<SysAppMenu> page = new Page<SysAppMenu>(pageNo, pageSize);
        IPage<SysAppMenu> pageList = sysAppMenuService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @GetMapping(value = "/showBunding")
    public Result<?> showBunding(String currentRoleId,
                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                    HttpServletRequest req) {
        //查询菜单角色表中绑定的菜单
        LambdaQueryWrapper<WmsPdaRoleMenu> query = new LambdaQueryWrapper<>();
        query.eq(WmsPdaRoleMenu::getRoleId,currentRoleId);
        List<WmsPdaRoleMenu> pdaMenus = wmsPdaRoleMenuService.list(query);
        List<String> menuIds = new ArrayList<>();
        //没有中间表数据暂时不查询
        if(!CollectionUtils.isEmpty(pdaMenus)){
            menuIds = pdaMenus.stream().map(WmsPdaRoleMenu::getMenuId).collect(Collectors.toList());
        }
        SysAppMenu sysAppMenu = new SysAppMenu();
        QueryWrapper<SysAppMenu> queryWrapper = QueryGenerator.initQueryWrapper(sysAppMenu, req.getParameterMap());
        if(!CollectionUtils.isEmpty(menuIds)){
            queryWrapper.notIn("id",menuIds);
        }
        Page<SysAppMenu> page = new Page<SysAppMenu>(pageNo, pageSize);
        IPage<SysAppMenu> pageList = sysAppMenuService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @PostMapping(value = "/bunding")
    public Result<?> bunding(String pdaMenuIds,String currentRoleId) {
        String[] pms = pdaMenuIds.split(",");
        for (int i = 0; i < pms.length ; i++) {
            //防止重复添加
            LambdaQueryWrapper<WmsPdaRoleMenu> query = new LambdaQueryWrapper();
            query.eq(WmsPdaRoleMenu::getRoleId,currentRoleId);
            query.eq(WmsPdaRoleMenu::getMenuId,pms[i]);
            WmsPdaRoleMenu dto = wmsPdaRoleMenuService.getOne(query);
            if(!ObjectUtils.isEmpty(dto)){
                continue;
            }
            WmsPdaRoleMenu wwc = new WmsPdaRoleMenu();
            wwc.setMenuId(pms[i]);
            wwc.setRoleId(currentRoleId);
            wmsPdaRoleMenuService.save(wwc);
        }
        return Result.OK("绑定成功！",currentRoleId);
    }
   /**
    *   添加
    *
    * @param sysAppMenu
    * @return
    */
   @AutoLog(value = "PDA菜单信息-添加")
   @ApiOperation(value="PDA菜单信息-添加", notes="PDA菜单信息-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody SysAppMenu sysAppMenu) {
       sysAppMenuService.save(sysAppMenu);
       return Result.OK("添加成功！",sysAppMenu);
   }

   /**
    *  编辑
    *
    * @param sysAppMenu
    * @return
    */
   @AutoLog(value = "PDA菜单信息-编辑")
   @ApiOperation(value="PDA菜单信息-编辑", notes="PDA菜单信息-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody SysAppMenu sysAppMenu) {
       sysAppMenuService.updateById(sysAppMenu);
       return Result.OK("编辑成功!",sysAppMenu);
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "PDA菜单信息-通过id删除")
   @ApiOperation(value="PDA菜单信息-通过id删除", notes="PDA菜单信息-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       sysAppMenuService.removeById(id);
       return Result.OK("删除成功!",id);
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "PDA菜单信息-批量删除")
   @ApiOperation(value="PDA菜单信息-批量删除", notes="PDA菜单信息-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.sysAppMenuService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!",ids);
   }

   //权限绑定删除
   @DeleteMapping(value = "/deleteBunding")
   public Result<?> deleteBunding(@RequestParam(name="ids",required=true) String ids) {
       LambdaQueryWrapper<WmsPdaRoleMenu> query = new LambdaQueryWrapper<>();
       query.in(WmsPdaRoleMenu::getMenuId,Arrays.asList(ids.split(",")));
       wmsPdaRoleMenuService.remove(query);
       return Result.OK("批量删除成功!",ids);
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "PDA菜单信息-通过id查询")
   @ApiOperation(value="PDA菜单信息-通过id查询", notes="PDA菜单信息-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       SysAppMenu sysAppMenu = sysAppMenuService.getById(id);
       if(sysAppMenu==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(sysAppMenu);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param sysAppMenu
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, SysAppMenu sysAppMenu) {
       return super.exportXls(request, sysAppMenu, SysAppMenu.class, "PDA菜单信息");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, SysAppMenu.class);
   }

}
